package com.apk.tools;

import java.io.File;
import java.util.Objects;

import java_util.coo_util.Log.C_Console;
import java_util.coo_util.Log.C_Log;
import java_util.coo_util.Log.C_LogTxtSave;


public class C_main {

    /**
     * @param para 0=工作路径,1=打包类型,2=buildId
     * @throws Exception
     */
    public static void main(String[] para) throws Exception {
        C_cmd.S_Init();
        String currentPath = System.getProperty("user.dir") + "/";
        //currentPath = "D:/app2build/";

        String rootPath = null;
        if (para == null || para.length == 0 || para[0] == null) {
            rootPath = currentPath;
        } else {
            rootPath = para[0];
            if (!rootPath.endsWith("/") && !rootPath.endsWith("\\")) {
                rootPath += "/";
            }
            File file = new File(rootPath);
            if (!file.exists()) {
                System.err.println("参数输入的路径不存在........" + rootPath);
                return;// C_ReturnsResult.em1_pathNull;
            }
        }
        int buildType = 5;
        assert para != null;
        if (para.length >= 2) {
            try {
                buildType = Integer.parseInt(para[1].trim());
                if (buildType != 2 && buildType != 3) {
                    System.err.print("无法打包....打包类型不明确.." + para[1]);
                    return;
                }
            } catch (Exception ex) {
                System.err.print("解析类型错误......" + para[1]);
            }
        }
        String buildId = "0";
        if (para.length >= 3) {
            buildId = para[2];
        }
        S_GetNumber(rootPath);
        S_main(rootPath, buildType, buildId);

        C_ProgressReport.S_SendEnd();
    }

    static void S_GetNumber(String rootPath) {
        File fileApk = new File(rootPath + "app.apk");
        int number = 0;
        if (fileApk.exists()) {
            number++;
        }
        File fileIpa = new File(rootPath + "app.ipa");
        if (fileIpa.exists()) {
            number++;
        }
        int const0 = 0;
        File file = new File(rootPath);
        File[] tempList = file.listFiles();
        for (int i = 0; i < Objects.requireNonNull(tempList).length; i++) {
            if (tempList[i].isDirectory()) {
                String fileName = tempList[i].getPath() + "/bulid_config.txt";
                if (new File(fileName).exists()) {
                    const0++;
                }
            }
        }
        C_ProgressReport.o_total = number * const0 * 5;
        C_ProgressReport.o_currentProgress = 0;
    }

    static int S_main(String rootPath, int buildType, String buildId) throws Exception {
        C_Console console = new C_Console().S_Init(true, 10, "buildLog", rootPath + build_config._build);

        C_cmd.S_GotoPath(rootPath);
        C_Log.LogWarningFormat("当前路径...." + rootPath);
        File file = new File(rootPath);
        File[] tempList = file.listFiles();
        File fileApk = new File(rootPath + "app.apk");
        if (fileApk.exists() && (buildType != 3 || buildType >= 5)) {
            File keystore = new File(rootPath + "sign.keystore");
            if (!keystore.exists()) {
                C_Log.LogWarningFormat("签名文件不存在.无法打包...." + rootPath + "sign.keystore");
            }
            assert tempList != null;
            for (int i = 0; i < tempList.length; i++) {
                if (tempList[i].isDirectory()) {
                    C_Log.LogWarningFormat("main.开始打包......%d......%s.....", i, tempList[i].getPath());
                    try {
                        C_Apk2Sign.S_build(build_config._build, i, rootPath, tempList[i], buildId);
                    } catch (Exception ex) {
                        C_Log.LogErrorInfo("错误", ex, true);
                    }
                }
            }
        } else {
            C_Log.LogWarningFormat("apk不存在...." + rootPath + "app.apk");
        }
        if (C_cmd.c_isLinux && (buildType != 2 || buildType >= 5)) {
            File fileIpa = new File(rootPath + "app.ipa");
            if (fileIpa.exists()) {
                String xcodeTemplatePath = C_IpaUtil.S_CopyXcodeTemplate(rootPath + build_config._build);
                System.out.println("存在...." + fileIpa.getPath());
                assert tempList != null;
                for (int i = 0; i < tempList.length; i++) {
                    if (tempList[i].isDirectory()) {
                        C_Log.LogFormat("main.开始打包......%d......%s.....", i, tempList[i].getPath());
                        try {
                            C_Ipa2Sign.S_build(build_config._build + "/", xcodeTemplatePath, i, rootPath, tempList[i], buildId);
                        } catch (Exception ex) {
                            C_Log.LogErrorInfo("错误", ex, true);
                        }
                    }
                }
                C_IpaUtil.S_DelectXcodeTemplate(rootPath + build_config._build);
            } else {
                C_Log.LogWarningFormat("ipa不存在...." + rootPath + "app.ipa");
            }
        }
        C_Log.Log("\n\n\n\n\n\n\n\n\n\n--------------打包.完成__谢谢合作_____\n\n\n\n\n\n\n\n\n\n");

        C_LogTxtSave.S_SaveData();
        return C_ReturnsResult.e01_success;
    }

    //程序执行返回的结果
    static class C_ReturnsResult {
        //工作路径不存在
        public static final int em1_pathNull = -1;

        //执行完成
        public static final int e01_success = 1;
    }


}
